package org.dolphin.entity.promo;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.dolphin.base.constant.DatabaseAttribute;
import org.dolphin.entity.interceptor.Creatable;
import org.dolphin.entity.security.User;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "MS_VOUCHER_BATCH")
public class VoucherBatch implements Serializable, Creatable {
  private static final long serialVersionUID = 4145369048663685229L;
  private String id;
  private Integer total;
  private String remarks;
  private User createdBy;
  private Date createdDate;
  private List<Voucher> vouchers;

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof User) {
      User oth = (User) obj;
      return ObjectUtils.nullSafeEquals(getId(), oth.getId());
    }
    return false;
  }

  @Override
  @ManyToOne(fetch = FetchType.LAZY, optional = true)
  @JoinColumn(name = "CREATED_BY", nullable = true)
  @ForeignKey(name = "FK_MS_VOUCHER_BATCH_CREATED_BY")
  public User getCreatedBy() {
    return createdBy;
  }

  @Override
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "CREATED_DATE", nullable = false)
  public Date getCreatedDate() {
    return createdDate;
  }

  @Id
  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "org.dolphin.base.id.generator.CustomUUIDGenerator")
  @Column(name = "ID", nullable = false, length = DatabaseAttribute.ID_LENGTH)
  public String getId() {
    return id;
  }

  @Column(name = "REMARKS", nullable = false, length = 500)
  public String getRemarks() {
    return remarks;
  }

  @Column(name = "TOTAL_VOUCHER", nullable = false)
  public Integer getTotal() {
    return total;
  }

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "voucherBatch")
  @Fetch(FetchMode.SUBSELECT)
  public List<Voucher> getVouchers() {
    return vouchers;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getId());
    return hash;
  }

  @Override
  public void setCreatedBy(User createdBy) {
    this.createdBy = createdBy;
  }

  @Override
  public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
  }

  public void setId(String id) {
    this.id = id;
  }

  public void setRemarks(String remarks) {
    this.remarks = remarks;
  }

  public void setTotal(Integer total) {
    this.total = total;
  }

  public void setVouchers(List<Voucher> vouchers) {
    this.vouchers = vouchers;
  }
}
